package com.zkk.tankgame02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * 坦克大战的绘图区域
 * */
public class MyPanel extends JPanel implements KeyListener {
    //定义我的坦克
    MyTank myTank = null;
    //定义敌人坦克,放入vector(多线程)
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTanksNum = 3;


    public MyPanel(){
        myTank = new MyTank(100,100);//初始化自己的坦克

        //初始化敌人坦克
        for(int i = 0;i < enemyTanksNum;i++){
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 100);
            enemyTank.setDirect(2);
            enemyTanks.add(enemyTank);
        }

        myTank.setSpeed(4);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.fillRect(0,0,1000,750);//填充矩形默认黑色

        //画坦克--封装成方法
        drawTank(myTank.getX(),myTank.getY(),g,myTank.getDirect(),myTank.getType());
        for (int i = 0; i < enemyTanks.size(); i++) {
            //取出坦克
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),enemyTank.getType());
        }


    }
    //画坦克

    /**
     *
     * @param x 坦克的左上角x坐标
     * @param y 坦克的左上角y坐标
     * @param g 画笔
     * @param direct 坦克方向（上下左右）
     * @param type  坦克类型（敌方？ 我方？）
     */
    public void drawTank(int x,int y,Graphics g,int direct,int type){
    //1.根据不同类型的坦克，设置不同的颜色
        switch(type){

            case 0://0:我方坦克
                g.setColor(Color.PINK);
                break;
            case 1://1：敌方坦克
                g.setColor(Color.yellow);
                break;
        }

    //2.根据坦克的初始方向，来绘制坦克
        //direct(0:向上 1：向右 2：向下 3向左 )
        switch(direct){
            case 0://表示向上
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x + 30,y,10,60,false);
                g.fill3DRect(x + 10,y + 10,20,40,false);
                g.fillOval(x + 10,y + 20,20,20);
                g.drawLine(x + 20,y + 30,x + 20,y);
                break;
            case 1://右
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y + 30,60,10,false);
                g.fill3DRect(x + 10,y + 10,40,20,false);
                g.fillOval(x + 20,y + 10,20,20);
                g.drawLine(x + 30,y + 20,x + 60,y + 20);
                break;
            case 2://下
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x + 30,y,10,60,false);
                g.fill3DRect(x + 10,y + 10,20,40,false);
                g.fillOval(x + 10,y + 20,20,20);
                g.drawLine(x + 20,y + 30,x + 20,y + 60);
                break;
            case 3:
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y + 30,60,10,false);
                g.fill3DRect(x + 10,y + 10,40,20,false);
                g.fillOval(x + 20,y + 10,20,20);
                g.drawLine(x + 30,y + 20,x,y + 20);
                break;
            default:
                System.out.println("暂时无处理....");

        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W){//上
            //改变方向
            myTank.setDirect(0);
            myTank.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_D){//右
            myTank.setDirect(1);
            myTank.moveRight();
        }else if(e.getKeyCode() == KeyEvent.VK_S){//下
            myTank.setDirect(2);
            myTank.moveDown();
        }else if(e.getKeyCode() == KeyEvent.VK_A){//左
            myTank.setDirect(3);
            myTank.moveLeft();
        }
        //重绘
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
